﻿/*
* Copyright (C) 2020 Tianjin KYLIN Information Technology Co., Ltd.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/&gt;.
*
*/
#include "shortcutpanelwidget.h"

shortcutPanelWidget::shortcutPanelWidget(QObject *parent)
    : m_pMainVLayout(new QVBoxLayout),
      m_pShortGLayout(new QGridLayout),
      m_PartLine(new PartLineWidget),
      m_pShortWidget(new QWidget),
      m_pScrollingAreaWidgetMajor(new ScrollingAreaWidgetMajor)
{
    Q_UNUSED(parent);
    initMemberVariablesMajor();      // 初始化插件成员变量
    initShortButtonWidgetMajor();    // 初始化快捷按钮界面
    initsetShortWidget();            // 布局快捷按钮界面
    setWidgetMajor();                // 将切换按钮和ListView界面set进插件主界面
}


// 布局快捷按钮界面
void shortcutPanelWidget::initsetShortWidget()
{
    int tmpx = 0, tmpy = 0;

    for (int i = 0; i < m_shortButtonWidgetList.count(); i++) {
        m_pShortGLayout->addWidget(m_shortButtonWidgetList.at(i), tmpx, tmpy, 1, 1);
        tmpy++;
        if (tmpy == 4) {
            tmpy = 0;
            tmpx++;
        }
    }
    return;
}

// 初始化3.1插件成员变量
void shortcutPanelWidget::initMemberVariablesMajor()
{
    m_pMainVLayout->setContentsMargins(0, 0, 0, 0);
    m_pMainVLayout->setSpacing(0);

    m_pShortGLayout->setContentsMargins(10, 16, 26, 16);
    m_pShortGLayout->setHorizontalSpacing(15);
    m_pShortGLayout->setVerticalSpacing(16);

    /* 快捷操作面板界面 */
    m_pShortWidget->setFixedHeight(227);
    m_pShortWidget->setContentsMargins(0, 0, 0, 0);

    /* 分割线 */
    m_PartLine->setFixedSize(MAJOR_QUICK_PANEL_WIDTH, 1);
    m_PartLine->setContentsMargins(0, 0, 0, 0);

    m_pScrollingAreaWidgetMajor->grabKeyboard();

    this->setContentsMargins(0, 0, 0, 0);
    this->setFixedSize(MAJOR_QUICK_PANEL_WIDTH, MAJOR_QUICK_PANEL_HEIGHT);
    return;
}

// 初始化3.1快捷按钮界面
void shortcutPanelWidget::initShortButtonWidgetMajor()
{
    m_shortButtonWidgetList.clear();

    QStringList buttonName;
    checkFunctionValid(EYE_PROTECTION_NAME,buttonName);
    checkFunctionValid(FLIGHTMODE_NAME,buttonName);
    checkFunctionValid(PROJECTSCREEN_NAME,buttonName);
    checkFunctionValid(CLOCK_NAME,buttonName);
    checkFunctionValid(SCREENSHOT_NAME,buttonName);
    checkFunctionValid(NOTEBOOK_NAME,buttonName);
    checkFunctionValid(SUPPORT_NAME,buttonName);
    checkFunctionValid(CLIPBOARD_NAME,buttonName);

    for(int i = 0; i < buttonName.count(); i++) {
        templatewidget *m_ptemplatewidget = new templatewidget(buttonName.at(i));
        m_shortButtonWidgetList.append(m_ptemplatewidget);
    }
}


void shortcutPanelWidget::setWidgetMajor()
{
    // 栅格布局加入widget
    m_pShortWidget->setLayout(m_pShortGLayout);

    // 总体布局
    m_pMainVLayout->addWidget(m_pShortWidget);               //快捷按钮
    m_pMainVLayout->addWidget(m_PartLine);
    m_pMainVLayout->addWidget(m_pScrollingAreaWidgetMajor);  // 滚动条区域
    m_pMainVLayout->addItem(new QSpacerItem(50, 8, QSizePolicy::Expanding));
    this->setLayout(m_pMainVLayout);

    return;
}

void shortcutPanelWidget::checkFunctionValid(QString function, QStringList &functionList)
{
    //因为不同组件机制不同，一些功能暂时无法确定是否为可用状态，此部分代码持续跟进
    if(function == SETTING_NAME) {
        //系统设置
    } else if (function == POWERSAVING_NAME) {
        //省电模式
    } else if (function == BLUETOOTH_NAME) {
        //蓝牙
        if (BluetoothDbus::getInstance()->getDefaultAdapterAddress() == "") {
            return ;
        }
    } else if (function == FLIGHTMODE_NAME) {
        //飞行模式
        if (SettingsDaemonMediaGsetting::getInstance()->getRfkillState() == -1) {
            return ;
        }
    } else if (function == PROJECTSCREEN_NAME) {
        //投屏
    } else if (function == EYE_PROTECTION_NAME) {
        //护眼模式
    } else if (function == SCREENSHOT_NAME) {
        //截图
    } else if (function == CLIPBOARD_NAME) {
        //剪贴板
        // 默认不注册，直接返回
        return;
    } else if (function == NODISTURB_NAME) {
        //勿扰模式
    } else if (function == ROTATION_NAME) {
        //自动旋转
    } else if (function == PAD_NAME) {
        //平板模式
    }

    functionList.append(function);
}

PartLineWidget::PartLineWidget(QWidget *parent) : QWidget(parent)
{
    initStyle();

}

void PartLineWidget::initStyle()
{
    m_pStyleName =  StyleGsetting::getInstance()->getStyleName();
    connect(StyleGsetting::getInstance(),&StyleGsetting::Sig_styleGsettingChanged,[=](QString key){
        if (key == UKUI_STYLE_NAME_KEY) {
            m_pStyleName =  StyleGsetting::getInstance()->getStyleName();
        }
    });
}

void PartLineWidget::paintEvent(QPaintEvent *event)
{
    QPainter p(this);
    QRect rect = this->rect();
    p.setRenderHint(QPainter::Antialiasing);  // 反锯齿;
    QColor color=qApp->palette().color(QPalette::Base);

    if (m_pStyleName == "ukui-dark") {
        color.setRgb(255,255,255,255);
    } else {
        color.setRgb(1,1,1,255);
    }

    p.setBrush(color);
    p.setOpacity(0.05);
    p.setPen(Qt::NoPen);
    p.drawRoundedRect(rect,0,0);

    QWidget::paintEvent(event);
}
